<template>
  <div class="completed-cases">
    <div class="page-header">
      <h1 class="page-title">鉴定完成案件</h1>
      <div class="header-actions">
        <el-button type="success" @click="exportReports">
          <i class="fas fa-download mr-2"></i>导出报告
        </el-button>
        <el-button type="primary" @click="refreshData">
          <i class="fas fa-sync-alt mr-2"></i>刷新
        </el-button>
      </div>
    </div>

    <!-- 搜索和筛选 -->
    <div class="search-section">
      <div class="search-row">
        <el-input
          v-model="searchQuery"
          placeholder="搜索案件编号、客户姓名或鉴定结果"
          class="search-input"
          clearable
        >
          <template #prefix>
            <i class="fas fa-search"></i>
          </template>
        </el-input>
        <el-select v-model="resultFilter" placeholder="鉴定结果" clearable class="result-filter">
          <el-option label="全部" value=""></el-option>
          <el-option label="有效" value="valid"></el-option>
          <el-option label="无效" value="invalid"></el-option>
          <el-option label="部分有效" value="partial"></el-option>
        </el-select>
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="date-picker"
          clearable
        />
        <el-button type="primary" @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <!-- 统计信息 -->
    <div class="stats-section">
      <div class="stat-card total">
        <div class="stat-icon">
          <i class="fas fa-check-circle"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ stats.totalCompleted }}</div>
          <div class="stat-label">已完成案件</div>
        </div>
      </div>
      <div class="stat-card valid">
        <div class="stat-icon">
          <i class="fas fa-thumbs-up"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ stats.validCases }}</div>
          <div class="stat-label">有效鉴定</div>
        </div>
      </div>
      <div class="stat-card partial">
        <div class="stat-icon">
          <i class="fas fa-balance-scale"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ stats.partialCases }}</div>
          <div class="stat-label">部分有效</div>
        </div>
      </div>
      <div class="stat-card invalid">
        <div class="stat-icon">
          <i class="fas fa-times-circle"></i>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ stats.invalidCases }}</div>
          <div class="stat-label">无效鉴定</div>
        </div>
      </div>
    </div>

    <!-- 案件列表 -->
    <div class="cases-section">
      <div class="section-header">
        <h2 class="section-title">已完成案件列表</h2>
        <div class="view-controls">
          <el-radio-group v-model="viewMode" size="small">
            <el-radio-button label="table">表格视图</el-radio-button>
            <el-radio-button label="card">卡片视图</el-radio-button>
          </el-radio-group>
        </div>
      </div>

      <!-- 表格视图 -->
      <div v-if="viewMode === 'table'" class="table-view">
        <el-table :data="filteredCases" v-loading="loading" style="width: 100%">
          <el-table-column prop="caseNumber" label="案件编号" width="120"></el-table-column>
          <el-table-column prop="clientName" label="客户姓名" width="100"></el-table-column>
          <el-table-column prop="caseType" label="案件类型" width="100"></el-table-column>
          <el-table-column prop="expertName" label="鉴定专家" width="100"></el-table-column>
          <el-table-column prop="result" label="鉴定结果" width="100" align="center">
            <template #default="{ row }">
              <el-tag :type="getResultType(row.result)">{{ getResultText(row.result) }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="completedDate" label="完成日期" width="120"></el-table-column>
          <el-table-column prop="compensationAmount" label="赔偿金额" width="120" align="center">
            <template #default="{ row }">
              <span class="text-green-600 font-semibold">
                {{ row.compensationAmount ? `¥${row.compensationAmount.toLocaleString()}` : '-' }}
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="compensationCoefficient" label="赔偿系数" width="120" align="center">
            <template #default="{ row }">
              <div class="coefficient-display">
                <span class="coefficient-value" :class="getCoefficientClass(row.compensationCoefficient)">
                  {{ row.compensationCoefficient || '-' }}
                </span>
                <el-button 
                  v-if="row.compensationAmount"
                  size="mini" 
                  type="text" 
                  @click="editCoefficient(row)"
                  class="coefficient-edit-btn"
                >
                  <i class="fas fa-edit"></i>
                </el-button>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="summary" label="鉴定摘要" min-width="200"></el-table-column>
          <el-table-column label="案件信息" width="200" align="center">
            <template #default="{ row }">
              <el-button size="small" type="text" @click="showCaseInfo(row)">
                <i class="fas fa-info-circle mr-1"></i>详细信息
              </el-button>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180" align="center">
            <template #default="{ row }">
              <el-button size="small" type="primary" @click="viewReport(row)">
                <i class="fas fa-file-alt mr-1"></i>查看报告
              </el-button>
              <el-button size="small" type="success" @click="downloadReport(row)">
                <i class="fas fa-download mr-1"></i>下载
              </el-button>
              <el-button size="small" type="info" @click="viewCaseDetail(row)">
                <i class="fas fa-eye mr-1"></i>详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 卡片视图 -->
      <div v-else class="card-view">
        <div class="cases-grid">
          <div 
            v-for="case_ in filteredCases" 
            :key="case_.id"
            class="case-card"
            :class="getResultClass(case_.result)"
          >
            <div class="case-header">
              <div class="case-number">{{ case_.caseNumber }}</div>
              <el-tag :type="getResultType(case_.result)" size="small">
                {{ getResultText(case_.result) }}
              </el-tag>
            </div>
            
            <div class="case-info">
              <h3 class="client-name">{{ case_.clientName }}</h3>
              <p class="case-type">{{ case_.caseType }}</p>
              <p class="expert-name">鉴定专家：{{ case_.expertName }}</p>
              <p class="case-summary">{{ case_.summary }}</p>
            </div>
            
            <div class="case-details">
              <div class="detail-item">
                <i class="fas fa-calendar-check"></i>
                <span>完成：{{ case_.completedDate }}</span>
              </div>
              <div class="detail-item" v-if="case_.compensationAmount">
                <i class="fas fa-money-bill-wave"></i>
                <span class="text-green-600">赔偿：¥{{ case_.compensationAmount.toLocaleString() }}</span>
              </div>
              <div class="detail-item" v-if="case_.compensationCoefficient">
                <i class="fas fa-calculator"></i>
                <span :class="getCoefficientClass(case_.compensationCoefficient)">
                  系数：{{ case_.compensationCoefficient }}
                </span>
              </div>
              <div class="detail-item">
                <i class="fas fa-star"></i>
                <span>评分：{{ case_.rating }}/5</span>
              </div>
            </div>
            
            <div class="case-actions">
              <el-button size="small" type="primary" @click="viewReport(case_)">
                <i class="fas fa-file-alt mr-1"></i>查看报告
              </el-button>
              <el-button size="small" type="success" @click="downloadReport(case_)">
                <i class="fas fa-download mr-1"></i>下载
              </el-button>
              <el-button 
                v-if="case_.compensationAmount"
                size="small" 
                type="warning" 
                @click="editCoefficient(case_)"
              >
                <i class="fas fa-calculator mr-1"></i>系数
              </el-button>
              <el-button size="small" type="info" @click="showCaseInfo(case_)">
                <i class="fas fa-info-circle mr-1"></i>案件信息
              </el-button>
              <el-button size="small" type="info" @click="viewCaseDetail(case_)">
                <i class="fas fa-eye mr-1"></i>详情
              </el-button>
            </div>
          </div>
        </div>
      </div>

      <!-- 分页 -->
      <div class="pagination-section">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalCases"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 案件信息详情对话框 -->
    <el-dialog 
      v-model="caseInfoVisible" 
      :title="`案件详细信息 - ${selectedCase?.caseNumber}`" 
      width="800px"
      @close="closeCaseInfo"
    >
      <div v-if="selectedCase" class="case-info-dialog">
        <!-- 基本信息 -->
        <div class="info-section">
          <h3 class="section-title">
            <i class="fas fa-info-circle mr-2"></i>
            基本信息
          </h3>
          <div class="info-grid">
            <div class="info-item">
              <span class="info-label">案件编号：</span>
              <span class="info-value">{{ selectedCase.caseNumber }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">客户姓名：</span>
              <span class="info-value">{{ selectedCase.clientName }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">案件类型：</span>
              <span class="info-value">{{ selectedCase.caseType }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">鉴定专家：</span>
              <span class="info-value">{{ selectedCase.expertName }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">完成日期：</span>
              <span class="info-value">{{ selectedCase.completedDate }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">鉴定结果：</span>
              <el-tag :type="getResultType(selectedCase.result)">
                {{ getResultText(selectedCase.result) }}
              </el-tag>
            </div>
          </div>
        </div>

        <!-- 事故信息 -->
        <div class="info-section">
          <h3 class="section-title">
            <i class="fas fa-car-crash mr-2"></i>
            事故信息
          </h3>
          <div class="info-grid">
            <div class="info-item">
              <span class="info-label">事故时间：</span>
              <span class="info-value">{{ selectedCase.accidentInfo?.accidentTime || '2024-01-15 14:30' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">事故地点：</span>
              <span class="info-value">{{ selectedCase.accidentInfo?.accidentLocation || '上海市浦东新区世纪大道与张杨路交叉口' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">事故类型：</span>
              <span class="info-value">{{ selectedCase.accidentInfo?.accidentType || '机动车追尾碰撞' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">责任认定：</span>
              <span class="info-value text-red-600 font-semibold">{{ selectedCase.accidentInfo?.responsibility || '后车承担全部责任' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">天气情况：</span>
              <span class="info-value">{{ selectedCase.accidentInfo?.weather || '晴天' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">道路条件：</span>
              <span class="info-value">{{ selectedCase.accidentInfo?.roadCondition || '干燥、视线良好' }}</span>
            </div>
          </div>
        </div>

        <!-- 伤情信息 -->
        <div class="info-section">
          <h3 class="section-title">
            <i class="fas fa-user-injured mr-2"></i>
            伤情信息
          </h3>
          <div class="info-grid">
            <div class="info-item">
              <span class="info-label">主要受伤部位：</span>
              <span class="info-value">{{ selectedCase.injuryInfo?.mainInjury || '颈椎、腰椎、右腕关节' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">入院诊断：</span>
              <span class="info-value">{{ selectedCase.injuryInfo?.diagnosis || '颈椎挥鞭伤、腰椎压缩性骨折、右侧桡骨远端骨折' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">治疗情况：</span>
              <span class="info-value">{{ selectedCase.injuryInfo?.treatment || '颈托固定、腰椎手术内固定、右腕石膏固定' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">住院天数：</span>
              <span class="info-value">{{ selectedCase.injuryInfo?.hospitalizationDays || 28 }}天</span>
            </div>
            <div class="info-item">
              <span class="info-label">康复期：</span>
              <span class="info-value">{{ selectedCase.injuryInfo?.recoveryPeriod || '6-12个月' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">后续治疗：</span>
              <span class="info-value">{{ selectedCase.injuryInfo?.followUpTreatment || '定期复查、物理治疗' }}</span>
            </div>
          </div>
        </div>

        <!-- 鉴定详情 -->
        <div class="info-section">
          <h3 class="section-title">
            <i class="fas fa-clipboard-check mr-2"></i>
            鉴定详情
          </h3>
          <div class="appraisal-details">
            <div class="detail-card">
              <h4 class="detail-title">伤残等级</h4>
              <div class="detail-content">
                <div class="disability-level">
                  <span class="level-badge">{{ selectedCase.appraisalDetails?.disabilityLevel || '九级' }}</span>
                  <span class="level-description">{{ selectedCase.appraisalDetails?.disabilityDescription || '多处骨折，功能部分受限' }}</span>
                </div>
              </div>
            </div>
            
            <div class="detail-card">
              <h4 class="detail-title">三期鉴定</h4>
              <div class="detail-content">
                <div class="period-info">
                  <div class="period-item">
                    <span class="period-label">误工期：</span>
                    <span class="period-value">{{ selectedCase.appraisalDetails?.workPeriod || 180 }}天</span>
                  </div>
                  <div class="period-item">
                    <span class="period-label">护理期：</span>
                    <span class="period-value">{{ selectedCase.appraisalDetails?.carePeriod || 90 }}天</span>
                  </div>
                  <div class="period-item">
                    <span class="period-label">营养期：</span>
                    <span class="period-value">{{ selectedCase.appraisalDetails?.nutritionPeriod || 60 }}天</span>
                  </div>
                </div>
              </div>
            </div>
            
            <div class="detail-card">
              <h4 class="detail-title">赔偿计算</h4>
              <div class="detail-content">
                <div class="compensation-breakdown">
                  <div class="comp-item">
                    <span class="comp-label">医疗费：</span>
                    <span class="comp-value">¥{{ (selectedCase.compensationAmount * 0.35).toLocaleString() || '98,000' }}</span>
                  </div>
                  <div class="comp-item">
                    <span class="comp-label">误工费：</span>
                    <span class="comp-value">¥{{ (selectedCase.compensationAmount * 0.25).toLocaleString() || '70,000' }}</span>
                  </div>
                  <div class="comp-item">
                    <span class="comp-label">护理费：</span>
                    <span class="comp-value">¥{{ (selectedCase.compensationAmount * 0.15).toLocaleString() || '42,000' }}</span>
                  </div>
                  <div class="comp-item">
                    <span class="comp-label">残疾赔偿金：</span>
                    <span class="comp-value">¥{{ (selectedCase.compensationAmount * 0.25).toLocaleString() || '70,000' }}</span>
                  </div>
                  <div class="comp-item total">
                    <span class="comp-label">总计：</span>
                    <span class="comp-value">¥{{ selectedCase.compensationAmount?.toLocaleString() || '280,000' }}</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 材料清单 -->
        <div class="info-section">
          <h3 class="section-title">
            <i class="fas fa-folder-open mr-2"></i>
            鉴定材料
          </h3>
          <div class="materials-list">
            <div class="material-category">
              <h4 class="category-title">医疗材料</h4>
              <ul class="material-items">
                <li v-for="material in getMedicalMaterials()" :key="material" class="material-item">
                  <i class="fas fa-check-circle text-green-500 mr-2"></i>
                  {{ material }}
                </li>
              </ul>
            </div>
            <div class="material-category">
              <h4 class="category-title">法律材料</h4>
              <ul class="material-items">
                <li v-for="material in getLegalMaterials()" :key="material" class="material-item">
                  <i class="fas fa-check-circle text-green-500 mr-2"></i>
                  {{ material }}
                </li>
              </ul>
            </div>
            <div class="material-category">
              <h4 class="category-title">技术材料</h4>
              <ul class="material-items">
                <li v-for="material in getTechnicalMaterials()" :key="material" class="material-item">
                  <i class="fas fa-check-circle text-green-500 mr-2"></i>
                  {{ material }}
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="closeCaseInfo">关闭</el-button>
          <el-button type="primary" @click="printCaseInfo">
            <i class="fas fa-print mr-2"></i>打印信息
          </el-button>
          <el-button type="success" @click="exportCaseInfo">
            <i class="fas fa-download mr-2"></i>导出信息
          </el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 编辑赔偿系数对话框 -->
    <el-dialog 
      v-model="coefficientDialogVisible" 
      title="编辑赔偿系数" 
      width="500px"
      @close="closeCoefficientDialog"
    >
      <div v-if="currentEditCase" class="coefficient-dialog">
        <!-- 案件基本信息 -->
        <div class="case-basic-info">
          <h4 class="info-header">
            <i class="fas fa-info-circle text-blue-600 mr-2"></i>
            案件信息
          </h4>
          <div class="basic-info-grid">
            <div class="basic-info-item">
              <span class="basic-label">案件编号：</span>
              <span class="basic-value">{{ currentEditCase.caseNumber }}</span>
            </div>
            <div class="basic-info-item">
              <span class="basic-label">客户姓名：</span>
              <span class="basic-value">{{ currentEditCase.clientName }}</span>
            </div>
            <div class="basic-info-item">
              <span class="basic-label">当前赔偿金额：</span>
              <span class="basic-value compensation">
                ¥{{ currentEditCase.compensationAmount?.toLocaleString() }}
              </span>
            </div>
            <div class="basic-info-item">
              <span class="basic-label">鉴定结果：</span>
              <el-tag :type="getResultType(currentEditCase.result)">
                {{ getResultText(currentEditCase.result) }}
              </el-tag>
            </div>
          </div>
        </div>

        <!-- 系数设置 -->
        <div class="coefficient-setting">
          <h4 class="info-header">
            <i class="fas fa-calculator text-green-600 mr-2"></i>
            赔偿系数设置
          </h4>
          
          <!-- 系数输入 -->
          <div class="coefficient-input-section">
            <div class="form-group">
              <label class="form-label">赔偿系数</label>
              <el-input-number
                v-model="editForm.coefficient"
                :min="0.1"
                :max="3.0"
                :step="0.1"
                :precision="1"
                placeholder="请输入系数"
                class="coefficient-input"
              />
              <div class="coefficient-help">
                <span class="help-text">范围：0.1 - 3.0，步长：0.1</span>
              </div>
            </div>
            
            <!-- 系数说明 -->
            <div class="form-group">
              <label class="form-label">调整说明</label>
              <el-input
                v-model="editForm.reason"
                type="textarea"
                :rows="3"
                placeholder="请输入系数调整的原因和说明..."
                maxlength="200"
                show-word-limit
              />
            </div>
          </div>

          <!-- 系数说明卡片 -->
          <div class="coefficient-explanation">
            <h5 class="explanation-title">系数调整说明</h5>
            <div class="explanation-grid">
              <div class="explanation-item">
                <span class="range-badge high">1.5 - 3.0</span>
                <span class="range-desc">重大伤残、多重因素加重</span>
              </div>
              <div class="explanation-item">
                <span class="range-badge normal">0.8 - 1.4</span>
                <span class="range-desc">一般情况、标准赔偿范围</span>
              </div>
              <div class="explanation-item">
                <span class="range-badge low">0.1 - 0.7</span>
                <span class="range-desc">轻微伤残、责任减免</span>
              </div>
            </div>
          </div>

          <!-- 计算预览 -->
          <div class="calculation-preview" v-if="editForm.coefficient">
            <h5 class="preview-title">调整后预览</h5>
            <div class="preview-content">
              <div class="preview-item">
                <span class="preview-label">原赔偿金额：</span>
                <span class="preview-value original">
                  ¥{{ currentEditCase.compensationAmount?.toLocaleString() }}
                </span>
              </div>
              <div class="preview-item">
                <span class="preview-label">调整系数：</span>
                <span class="preview-value coefficient" :class="getCoefficientClass(editForm.coefficient)">
                  {{ editForm.coefficient }}
                </span>
              </div>
              <div class="preview-item total">
                <span class="preview-label">调整后金额：</span>
                <span class="preview-value adjusted">
                  ¥{{ getAdjustedAmount().toLocaleString() }}
                </span>
              </div>
              <div class="preview-item">
                <span class="preview-label">差额：</span>
                <span class="preview-value difference" :class="getDifferenceClass()">
                  {{ getDifferenceText() }}
                </span>
              </div>
            </div>
          </div>
        </div>
      </div>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="closeCoefficientDialog">取消</el-button>
          <el-button 
            type="primary" 
            @click="saveCoefficient"
            :disabled="!editForm.coefficient || !editForm.reason"
          >
            <i class="fas fa-save mr-2"></i>保存系数
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive, computed } from 'vue'

export default {
  name: 'CompletedCases',
  setup() {
    const loading = ref(false)
    const searchQuery = ref('')
    const resultFilter = ref('')
    const dateRange = ref([])
    const viewMode = ref('table')
    const currentPage = ref(1)
    const pageSize = ref(20)
    
    // 案件信息对话框相关
    const caseInfoVisible = ref(false)
    const selectedCase = ref(null)
    
    // 赔偿系数编辑相关
    const coefficientDialogVisible = ref(false)
    const currentEditCase = ref(null)
    const editForm = reactive({
      coefficient: null,
      reason: ''
    })
    
    const stats = reactive({
      totalCompleted: 162,   // 已完成交通事故鉴定总数
      validCases: 140,       // 有效的交通事故鉴定
      partialCases: 16,      // 部分有效的交通事故鉴定
      invalidCases: 6        // 无效的交通事故鉴定
    })
    
    const cases = ref([
      {
        id: 1,
        caseNumber: 'TA2024008',
        clientName: '张某',
        caseType: '交通事故',
        expertName: '李法医',
        result: 'valid',
        completedDate: '2024-01-25',
        compensationAmount: 280000,
        compensationCoefficient: 1.2,
        summary: '机动车追尾事故，被撞方构成九级伤残，误工期180天，护理期90天',
        rating: 4.8
      },
      {
        id: 2,
        caseNumber: 'TA2024009',
        clientName: '李某',
        caseType: '交通事故',
        expertName: '王专家',
        result: 'valid',
        completedDate: '2024-01-28',
        compensationAmount: 450000,
        compensationCoefficient: 1.8,
        summary: '酒驾肇事案件，被害人构成七级伤残，肇事方承担全部责任',
        rating: 4.9
      },
      {
        id: 3,
        caseNumber: 'TA2024010',
        clientName: '王某',
        caseType: '交通事故',
        expertName: '赵专家',
        result: 'partial',
        completedDate: '2024-01-30',
        compensationAmount: 95000,
        compensationCoefficient: 0.6,
        summary: '电动车与汽车碰撞，双方均有违规行为，电动车方承担40%责任',
        rating: 4.6
      },
      {
        id: 4,
        caseNumber: 'TA2024011',
        clientName: '陈某',
        caseType: '交通事故',
        expertName: '孙法医',
        result: 'valid',
        completedDate: '2024-02-02',
        compensationAmount: 680000,
        compensationCoefficient: 2.1,
        summary: '大货车侧翻压车事故，车内乘客构成六级伤残，需长期护理依赖',
        rating: 4.7
      },
      {
        id: 5,
        caseNumber: 'TA2024012',
        clientName: '刘某',
        caseType: '交通事故',
        expertName: '吴专家',
        result: 'valid',
        completedDate: '2024-02-05',
        compensationAmount: 38000,
        compensationCoefficient: 1.0,
        summary: '停车场内低速碰撞，被撞方构成十级伤残，误工期90天',
        rating: 4.4
      },
      {
        id: 6,
        caseNumber: 'TA2024013',
        clientName: '周某',
        caseType: '交通事故',
        expertName: '郑法医',
        result: 'invalid',
        completedDate: '2024-02-08',
        compensationAmount: null,
        compensationCoefficient: null,
        summary: '经鉴定，当事人伤情与交通事故无因果关系，系既往疾病所致',
        rating: 4.3
      }
    ])
    
    const filteredCases = computed(() => {
      return cases.value.filter(case_ => {
        const matchesSearch = !searchQuery.value || 
          case_.caseNumber.includes(searchQuery.value) ||
          case_.clientName.includes(searchQuery.value) ||
          case_.summary.includes(searchQuery.value)
        
        const matchesResult = !resultFilter.value || case_.result === resultFilter.value
        
        let matchesDate = true
        if (dateRange.value && dateRange.value.length === 2) {
          const caseDate = new Date(case_.completedDate)
          const startDate = new Date(dateRange.value[0])
          const endDate = new Date(dateRange.value[1])
          matchesDate = caseDate >= startDate && caseDate <= endDate
        }
        
        return matchesSearch && matchesResult && matchesDate
      })
    })
    
    const totalCases = computed(() => filteredCases.value.length)
    
    const getResultType = (result) => {
      const types = {
        'valid': 'success',
        'partial': 'warning',
        'invalid': 'danger'
      }
      return types[result] || 'info'
    }
    
    const getResultText = (result) => {
      const texts = {
        'valid': '有效',
        'partial': '部分有效',
        'invalid': '无效'
      }
      return texts[result] || '未知'
    }
    
    const getResultClass = (result) => {
      return `result-${result}`
    }
    
    const handleSearch = () => {
      currentPage.value = 1
    }
    
    const handleSizeChange = (size) => {
      pageSize.value = size
      currentPage.value = 1
    }
    
    const handleCurrentChange = (page) => {
      currentPage.value = page
    }
    
    const refreshData = () => {
      loading.value = true
      setTimeout(() => {
        loading.value = false
        alert('数据已刷新')
      }, 1000)
    }
    
    const exportReports = () => {
      alert('导出报告功能开发中...')
    }
    
    const viewReport = (case_) => {
      alert(`查看鉴定报告: ${case_.caseNumber}`)
    }
    
    const downloadReport = (case_) => {
      alert(`下载鉴定报告: ${case_.caseNumber}`)
    }
    
    const viewCaseDetail = (case_) => {
      alert(`查看案件详情: ${case_.caseNumber}`)
    }
    
    // 案件信息相关方法
    const showCaseInfo = (case_) => {
      selectedCase.value = case_
      caseInfoVisible.value = true
    }
    
    const closeCaseInfo = () => {
      caseInfoVisible.value = false
      selectedCase.value = null
    }
    
    const printCaseInfo = () => {
      if (!selectedCase.value) return
      window.print()
    }
    
    const exportCaseInfo = () => {
      if (!selectedCase.value) return
      alert(`导出案件 ${selectedCase.value.caseNumber} 信息功能开发中...`)
    }
    
    // 材料清单方法
    const getMedicalMaterials = () => {
      return [
        '门急诊病历资料',
        '住院病历资料', 
        'X线、CT、MRI影像资料',
        '诊断证明书',
        '出院小结',
        '手术记录',
        '康复记录'
      ]
    }
    
    const getLegalMaterials = () => {
      return [
        '身份证复印件',
        '道路交通事故认定书',
        '机动车驾驶证复印件',
        '机动车行驶证复印件',
        '保险单复印件',
        '现场照片及示意图'
      ]
    }
    
    const getTechnicalMaterials = () => {
      return [
        '车辆定损报告',
        '车辆维修发票',
        '医疗费发票及清单',
        '误工证明',
        '护理费证明',
        '营养费证明'
      ]
    }
    
    // 赔偿系数相关方法
    const getCoefficientClass = (coefficient) => {
      if (!coefficient) return 'coefficient-none'
      if (coefficient >= 1.5) return 'coefficient-high'
      if (coefficient >= 0.8) return 'coefficient-normal'
      return 'coefficient-low'
    }
    
    const editCoefficient = (case_) => {
      currentEditCase.value = case_
      editForm.coefficient = case_.compensationCoefficient || 1.0
      editForm.reason = ''
      coefficientDialogVisible.value = true
    }
    
    const closeCoefficientDialog = () => {
      coefficientDialogVisible.value = false
      currentEditCase.value = null
      editForm.coefficient = null
      editForm.reason = ''
    }
    
    const getAdjustedAmount = () => {
      if (!currentEditCase.value || !editForm.coefficient) return 0
      return Math.round(currentEditCase.value.compensationAmount * editForm.coefficient)
    }
    
    const getDifferenceClass = () => {
      const original = currentEditCase.value?.compensationAmount || 0
      const adjusted = getAdjustedAmount()
      const diff = adjusted - original
      
      if (diff > 0) return 'difference-positive'
      if (diff < 0) return 'difference-negative'
      return 'difference-zero'
    }
    
    const getDifferenceText = () => {
      const original = currentEditCase.value?.compensationAmount || 0
      const adjusted = getAdjustedAmount()
      const diff = adjusted - original
      
      if (diff === 0) return '无变化'
      const prefix = diff > 0 ? '+' : ''
      return `${prefix}¥${diff.toLocaleString()}`
    }
    
    const saveCoefficient = () => {
      if (!currentEditCase.value || !editForm.coefficient || !editForm.reason) {
        return
      }
      
      // 更新案件的赔偿系数
      const caseIndex = cases.value.findIndex(c => c.id === currentEditCase.value.id)
      if (caseIndex > -1) {
        cases.value[caseIndex].compensationCoefficient = editForm.coefficient
        // 这里可以记录调整历史
        cases.value[caseIndex].coefficientHistory = cases.value[caseIndex].coefficientHistory || []
        cases.value[caseIndex].coefficientHistory.push({
          date: new Date().toISOString().split('T')[0],
          coefficient: editForm.coefficient,
          reason: editForm.reason,
          operator: '系统管理员' // 实际应该从用户信息获取
        })
      }
      
      alert(`案件 ${currentEditCase.value.caseNumber} 的赔偿系数已更新为 ${editForm.coefficient}`)
      closeCoefficientDialog()
    }
    
    return {
      loading,
      searchQuery,
      resultFilter,
      dateRange,
      viewMode,
      currentPage,
      pageSize,
      stats,
      cases,
      filteredCases,
      totalCases,
      getResultType,
      getResultText,
      getResultClass,
      handleSearch,
      handleSizeChange,
      handleCurrentChange,
      refreshData,
      exportReports,
      viewReport,
      downloadReport,
      viewCaseDetail,
      caseInfoVisible,
      selectedCase,
      showCaseInfo,
      closeCaseInfo,
      printCaseInfo,
      exportCaseInfo,
      getMedicalMaterials,
      getLegalMaterials,
      getTechnicalMaterials,
      coefficientDialogVisible,
      currentEditCase,
      editForm,
      getCoefficientClass,
      editCoefficient,
      closeCoefficientDialog,
      getAdjustedAmount,
      getDifferenceClass,
      getDifferenceText,
      saveCoefficient
    }
  }
}
</script>

<style scoped>
.completed-cases {
  padding: 24px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.page-title {
  font-size: 28px;
  font-weight: bold;
  color: #1f2937;
  margin: 0;
}

.search-section {
  background: white;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.search-row {
  display: flex;
  gap: 16px;
  align-items: center;
}

.search-input {
  flex: 1;
  max-width: 400px;
}

.result-filter {
  width: 120px;
}

.date-picker {
  width: 240px;
}

.stats-section {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}

.stat-card {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  gap: 16px;
  border-left: 4px solid #e5e7eb;
}

.stat-card.total {
  border-left-color: #3b82f6;
}

.stat-card.valid {
  border-left-color: #10b981;
}

.stat-card.partial {
  border-left-color: #f59e0b;
}

.stat-card.invalid {
  border-left-color: #ef4444;
}

.stat-icon {
  width: 50px;
  height: 50px;
  border-radius: 8px;
  background: #f3f4f6;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: #6b7280;
}

.stat-content {
  flex: 1;
}

.stat-number {
  font-size: 24px;
  font-weight: bold;
  color: #1f2937;
  line-height: 1;
}

.stat-label {
  font-size: 14px;
  color: #6b7280;
  margin-top: 4px;
}

.cases-section {
  background: white;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.section-title {
  font-size: 20px;
  font-weight: bold;
  color: #1f2937;
  margin: 0;
}

.table-view {
  margin-bottom: 20px;
}

.cases-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.case-card {
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 20px;
  background: #fafafa;
  transition: all 0.3s ease;
}

.case-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.case-card.result-valid {
  border-left: 4px solid #10b981;
}

.case-card.result-partial {
  border-left: 4px solid #f59e0b;
}

.case-card.result-invalid {
  border-left: 4px solid #ef4444;
}

.case-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.case-number {
  font-size: 18px;
  font-weight: bold;
  color: #1f2937;
}

.case-info {
  margin-bottom: 16px;
}

.client-name {
  font-size: 16px;
  font-weight: bold;
  color: #1f2937;
  margin: 0 0 4px 0;
}

.case-type {
  font-size: 14px;
  color: #6b7280;
  margin: 0 0 4px 0;
}

.expert-name {
  font-size: 14px;
  color: #6b7280;
  margin: 0 0 8px 0;
}

.case-summary {
  font-size: 14px;
  color: #4b5563;
  margin: 0;
  line-height: 1.5;
}

.case-details {
  margin-bottom: 16px;
}

.detail-item {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
  font-size: 12px;
  color: #6b7280;
}

.detail-item i {
  width: 14px;
}

.case-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.pagination-section {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

/* 案件信息对话框样式 */
.case-info-dialog {
  max-height: 70vh;
  overflow-y: auto;
}

.info-section {
  margin-bottom: 24px;
  padding: 16px;
  background: #f8fafc;
  border-radius: 8px;
  border: 1px solid #e5e7eb;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #1f2937;
  margin: 0 0 16px 0;
  display: flex;
  align-items: center;
  padding-bottom: 8px;
  border-bottom: 1px solid #e5e7eb;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 12px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid #f3f4f6;
}

.info-item:last-child {
  border-bottom: none;
}

.info-label {
  font-weight: 500;
  color: #6b7280;
  font-size: 14px;
  min-width: 100px;
}

.info-value {
  color: #1f2937;
  font-size: 14px;
  font-weight: 500;
  text-align: right;
  flex: 1;
}

/* 鉴定详情样式 */
.appraisal-details {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 16px;
}

.detail-card {
  background: white;
  border-radius: 8px;
  padding: 16px;
  border: 1px solid #e5e7eb;
}

.detail-title {
  font-size: 14px;
  font-weight: 600;
  color: #374151;
  margin: 0 0 12px 0;
  padding-bottom: 8px;
  border-bottom: 1px solid #f3f4f6;
}

.detail-content {
  font-size: 14px;
}

/* 伤残等级样式 */
.disability-level {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.level-badge {
  display: inline-block;
  padding: 4px 12px;
  background: #3b82f6;
  color: white;
  border-radius: 20px;
  font-weight: 600;
  font-size: 14px;
  text-align: center;
  width: fit-content;
}

.level-description {
  color: #6b7280;
  font-size: 13px;
  line-height: 1.4;
}

/* 三期鉴定样式 */
.period-info {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.period-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 0;
}

.period-label {
  color: #6b7280;
  font-size: 13px;
}

.period-value {
  color: #1f2937;
  font-weight: 600;
  font-size: 13px;
}

/* 赔偿计算样式 */
.compensation-breakdown {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.comp-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 0;
  border-bottom: 1px solid #f3f4f6;
}

.comp-item:last-child {
  border-bottom: none;
}

.comp-item.total {
  border-top: 2px solid #3b82f6;
  margin-top: 8px;
  padding-top: 12px;
  font-weight: 600;
}

.comp-label {
  color: #6b7280;
  font-size: 13px;
}

.comp-item.total .comp-label {
  color: #1f2937;
  font-weight: 600;
}

.comp-value {
  color: #059669;
  font-weight: 600;
  font-size: 13px;
}

.comp-item.total .comp-value {
  color: #3b82f6;
  font-size: 16px;
}

/* 材料清单样式 */
.materials-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 16px;
}

.material-category {
  background: white;
  border-radius: 8px;
  padding: 16px;
  border: 1px solid #e5e7eb;
}

.category-title {
  font-size: 14px;
  font-weight: 600;
  color: #374151;
  margin: 0 0 12px 0;
  padding-bottom: 8px;
  border-bottom: 1px solid #f3f4f6;
}

.material-items {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.material-item {
  display: flex;
  align-items: center;
  font-size: 13px;
  color: #4b5563;
  line-height: 1.4;
}

/* 对话框底部样式 */
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding-top: 16px;
  border-top: 1px solid #e5e7eb;
}

/* 赔偿系数相关样式 */
.coefficient-display {
  display: flex;
  align-items: center;
  gap: 6px;
}

.coefficient-value {
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
}

.coefficient-value.coefficient-high {
  background: #fee2e2;
  color: #dc2626;
}

.coefficient-value.coefficient-normal {
  background: #dbeafe;
  color: #2563eb;
}

.coefficient-value.coefficient-low {
  background: #fef3c7;
  color: #d97706;
}

.coefficient-value.coefficient-none {
  color: #9ca3af;
}

.coefficient-edit-btn {
  padding: 0;
  margin: 0;
  color: #6b7280;
}

.coefficient-edit-btn:hover {
  color: #3b82f6;
}

/* 编辑赔偿系数对话框样式 */
.coefficient-dialog {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.case-basic-info {
  padding: 16px;
  background: #f8fafc;
  border-radius: 8px;
  border: 1px solid #e5e7eb;
}

.info-header {
  font-size: 16px;
  font-weight: 600;
  color: #1f2937;
  margin: 0 0 12px 0;
  display: flex;
  align-items: center;
  padding-bottom: 8px;
  border-bottom: 1px solid #e5e7eb;
}

.basic-info-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.basic-info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
}

.basic-label {
  font-size: 14px;
  color: #6b7280;
  font-weight: 500;
}

.basic-value {
  font-size: 14px;
  color: #1f2937;
  font-weight: 600;
}

.basic-value.compensation {
  color: #059669;
}

.coefficient-setting {
  background: white;
  border-radius: 8px;
  padding: 16px;
  border: 1px solid #e5e7eb;
}

.coefficient-input-section {
  margin-bottom: 20px;
}

.form-group {
  margin-bottom: 16px;
}

.form-label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  color: #374151;
  margin-bottom: 6px;
}

.coefficient-input {
  width: 100%;
}

.coefficient-help {
  margin-top: 4px;
}

.help-text {
  font-size: 12px;
  color: #6b7280;
}

/* 系数说明卡片 */
.coefficient-explanation {
  background: #f9fafb;
  border-radius: 8px;
  padding: 16px;
  border: 1px solid #e5e7eb;
  margin-bottom: 20px;
}

.explanation-title {
  font-size: 14px;
  font-weight: 600;
  color: #374151;
  margin: 0 0 12px 0;
}

.explanation-grid {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.explanation-item {
  display: flex;
  align-items: center;
  gap: 12px;
}

.range-badge {
  display: inline-block;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
  min-width: 60px;
  text-align: center;
}

.range-badge.high {
  background: #fee2e2;
  color: #dc2626;
}

.range-badge.normal {
  background: #dbeafe;
  color: #2563eb;
}

.range-badge.low {
  background: #fef3c7;
  color: #d97706;
}

.range-desc {
  font-size: 13px;
  color: #6b7280;
}

/* 计算预览 */
.calculation-preview {
  background: #f0f9ff;
  border-radius: 8px;
  padding: 16px;
  border: 1px solid #bae6fd;
}

.preview-title {
  font-size: 14px;
  font-weight: 600;
  color: #1e40af;
  margin: 0 0 12px 0;
}

.preview-content {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.preview-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid #e0f2fe;
}

.preview-item:last-child {
  border-bottom: none;
}

.preview-item.total {
  border-top: 2px solid #0ea5e9;
  margin-top: 8px;
  padding-top: 12px;
  font-weight: 600;
}

.preview-label {
  font-size: 14px;
  color: #374151;
}

.preview-value {
  font-size: 14px;
  font-weight: 600;
}

.preview-value.original {
  color: #6b7280;
}

.preview-value.coefficient {
  color: #2563eb;
}

.preview-value.adjusted {
  color: #059669;
  font-size: 16px;
}

.preview-value.difference {
  font-size: 13px;
}

.difference-positive {
  color: #dc2626;
}

.difference-negative {
  color: #059669;
}

.difference-zero {
  color: #6b7280;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .info-grid {
    grid-template-columns: 1fr;
  }
  
  .appraisal-details {
    grid-template-columns: 1fr;
  }
  
  .materials-list {
    grid-template-columns: 1fr;
  }
  
  .info-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
  
  .info-value {
    text-align: left;
  }
  
  .period-item,
  .comp-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
  
  .basic-info-grid {
    grid-template-columns: 1fr;
  }
  
  .basic-info-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
  
  .explanation-grid {
    gap: 12px;
  }
  
  .explanation-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
  }
}
</style> 